package edu.mse.camel.client.whiteboard.gef.ui.commands;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.util.EcoreUtil;

import edu.mse.camel.client.whiteboard.gef.whiteboard.Connection;
import edu.mse.camel.client.whiteboard.gef.whiteboard.Shape;
import edu.mse.camel.client.whiteboard.gef.whiteboard.WhiteboardDiagram;

/**
 * A command to remove a shape from its parent.
 * The command can be undone or redone.
 * @author darpan, choi
 */
public class ShapeDeleteCommand extends AbstractWhiteboardCommand {
	/** Shape to remove. */
	private final Shape child;
	
	/** ShapeDiagram to remove from. */
	private final WhiteboardDiagram parent;
	/** Holds a copy of the outgoing connections of child. */
	private List sourceConnections;
	/** Holds a copy of the incoming connections of child. */
	private List targetConnections;
	/** True, if child was removed from its parent. */
	private boolean wasRemoved;
	
	/**
	 * Create a command that will remove the shape from its parent.
	 * @param parent the ShapesDiagram containing the child
	 * @param child    the Shape to remove
	 * @throws IllegalArgumentException if any parameter is null
	 */
	public ShapeDeleteCommand(WhiteboardDiagram parent, Shape child) {
		if (parent == null || child == null) {
			throw new IllegalArgumentException();
		}
		setLabel("shape deletion");
		this.parent = parent;
		this.child = child;
	}
	
	/**
	 * Reconnects a List of Connections with their previous endpoints.
	 * @param connections a non-null List of connections
	 */
	private void addConnections(List connections) {
		for (Iterator iter = connections.iterator(); iter.hasNext();) {
			Connection conn = (Connection) iter.next();
			conn.getSource().getSourceConnections().add(conn);
			conn.getTarget().getTargetConnections().add(conn);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#canUndo()
	 */
	public boolean canUndo() {
		return wasRemoved;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		// store a copy of incoming & outgoing connections before proceeding 
		sourceConnections = new ArrayList(EcoreUtil.copyAll(child.getSourceConnections()));
		targetConnections = new ArrayList(EcoreUtil.copyAll(child.getTargetConnections()));
		redo();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		// remove the child and disconnect its connections
		wasRemoved = parent.getShapes().contains(child);
		if (wasRemoved) {
			removeConnections(child.getSourceConnections());
			removeConnections(child.getTargetConnections());
			parent.getShapes().remove(child);
		}
	}
	
	/**
	 * Disconnects a List of Connections from their endpoints.
	 * @param connections a non-null List of connections
	 */
	private void removeConnections(List connections) {
		for (int i = 0; i < connections.size(); i++) {
			Connection conn = (Connection) connections.get(i);
			conn.getSource().getSourceConnections().remove(conn);
			conn.getTarget().getTargetConnections().remove(conn);
		}
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		// add the child and reconnect its connections
		if (parent.getShapes().add(child)) {
			addConnections(sourceConnections);
			addConnections(targetConnections);
		}
	}
}